
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <base href="<%=basePath%>">
    <title>航班信息</title>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
      $(function () {
        $("#err").hide();
        $("#show").hide();
        citySelect();
        $("#search").click(function () {
            var departureCity=$("#departureCity").val();
            var arrivalCity=$("#arrivalCity").val();
            if(departureCity==""||arrivalCity==""){
                alert('请选择起飞城市和到达城市');
                return false;
            }else if(departureCity==arrivalCity){
                alert('起飞城市和达到城市不能相同');
                return false;
            }else{
                ajaxSearch(1);
            }
        })
      })

      function ajaxSearch(pageIndex) {
          var departureCity=$("#departureCity").val();
          var arrivalCity=$("#arrivalCity").val();
            $.post(
                "flight/show",
                {pageIndex:pageIndex,departureCity:departureCity,arrivalCity:arrivalCity},
                function (data) {
                    $("#pageIndex").html(data.pageIndex);
                    $("#pageCount").html(data.pageCount);
                    $("#totalCount").html(data.totalCount);
                    var datas=data.datas;
                    if(datas.length==0){
                        $("#err").show();
                        $("#show").hide();
                    }else{
                        $("#err").hide();
                        $("#show").show();
                        $("#show table tbody tr").remove();
                        $.each(datas,function (index,item) {
                            var tr='<tr>' +
                                ' <th>'+item.flightNo+'</th>\n' +
                                ' <th>'+item.departureCity.cityName+'</th>\n' +
                                ' <th>'+item.departureTime+'</th>\n' +
                                ' <th>'+item.arrivalCity.cityName+'</th>\n' +
                                ' <th>'+item.arrivalTime+'</th>\n' +
                                '  </tr>';
                            $("#show table tbody").append(tr);
                        })
                    }
                },"json"
            )
      }
    function first() {
        ajaxSearch(1);
    }

    function pre() {
        var pageIndex=$("#pageIndex").html();
        pageIndex--;
        if(pageIndex<=0){
            return false;
        }
        ajaxSearch(pageIndex);
    }
      function next() {
          var pageIndex=$("#pageIndex").html();
          var pageCount=$("#pageCount").html();
          pageIndex++;
          if(pageIndex>pageCount){
              return false;
          }
          ajaxSearch(pageIndex);
      }

      function last() {
          var pageCount=$("#pageCount").html();
          ajaxSearch(pageCount);
      }

      function citySelect() {
        $.post(
                "city/getAll",
                {},
                function (data) {
                  $("#departureCity option:gt(0)").remove();
                  $("#arrivalCity option:gt(0)").remove();
                  $.each(data,function (index,item) {
                      var option='<option value='+item.id+'>'+item.cityName+'</option>';
                    $("#departureCity").append(option);
                    $("#arrivalCity").append(option);
                  })
                },"json"
        )
      }
    </script>
  </head>
  <body>
        <h1>航班查询页面</h1>
        <p><a href="add.jsp">航班添加</a></p>
        起飞城市：
        <select id="departureCity">
            <option value="">--请选择起飞城市--</option>
        </select>
        ——到达城市：
        <select id="arrivalCity">
          <option value="">--请选择到达城市--</option>
        </select>
        &nbsp;&nbsp;
      <input type="button" id="search" value="查询"/>
        <h1 id="err">没有找到符合条件的航班，请选择查询条件重试</h1>
      <div id="show">
          <h1>帮您查找到一下航班</h1>
        <table>
              <thead>
                    <tr>
                        <th>航班编号</th>
                        <th>起飞城市</th>
                        <th>起飞时间</th>
                        <th>到达城市</th>
                        <th>到达时间</th>
                    </tr>
              </thead>
              <tbody></tbody>
              <tfoot>
                    <tr>
                        <td colspan="5">
                            <a href="javascript:first();">首页</a>
                            <a href="javascript:pre();">上一页</a>
                             <span id="pageIndex"></span>/<span id="pageCount"></span>
                            <a href="javascript:next();">下一页</a>
                            <a href="javascript:last();">尾页</a>
                          &nbsp;&nbsp;共<span id="totalCount"></span>条
                        </td>
                    </tr>
              </tfoot>
        </table>
      </div>
  </body>
</html>
